# Cache

<div class="api-docs__separator" data-reactroot="">

---

</div><div class="api-docs__import" data-reactroot="">

```ts
import { Cache } from "@hyper-fetch/core";
```

</div><div class="api-docs__section">

## Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Cache class handles the data exchange with the dispatchers.

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:23](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L23)

</p><div class="api-docs__section">

## Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="client"><td class="api-docs__param-name required">

### client

`Required`

</td><td class="api-docs__param-type">

`C`

</td></tr><tr param-data="options"><td class="api-docs__param-name optional">

### options

`Optional`

</td><td class="api-docs__param-type">

`CacheOptionsType<ClientInstance>`

</td></tr></tbody></table></div><div class="api-docs__section">

## Properties

</div><div class="api-docs__properties"><div class="api-docs__property" property-data="clearKey"><h3 class="api-docs__name">

### `clearKey`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:29](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L29)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
string;
```

</div><hr/></div><div class="api-docs__property" property-data="client"><h3 class="api-docs__name">

### `client`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:33](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L33)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
C;
```

</div><hr/></div><div class="api-docs__property" property-data="emitter"><h3 class="api-docs__name">

### `emitter`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:24](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L24)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
EventEmitter;
```

</div><hr/></div><div class="api-docs__property" property-data="events"><h3 class="api-docs__name">

### `events`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:25](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L25)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
{ emitCacheData: <Response,Error,Adapter>(cacheKey: string, data: CacheValueType<Response, Error, Adapter>) => void; emitRevalidation: (cacheKey: string) => void; onData: <Response,Error,Adapter>(cacheKey: string, callback: (data: CacheValueType<Response, Error, Adapter>) => void) => VoidFunction; onRevalidate: (cacheKey: string, callback: () => void) => VoidFunction }
```

</div><hr/></div><div class="api-docs__property" property-data="garbageCollectors"><h3 class="api-docs__name">

### `garbageCollectors`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:30](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L30)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
Map<string, Timeout>;
```

</div><hr/></div><div class="api-docs__property" property-data="lazyStorage"><h3 class="api-docs__name">

### `lazyStorage`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:28](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L28)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
CacheAsyncStorageType;
```

</div><hr/></div><div class="api-docs__property" property-data="options"><h3 class="api-docs__name">

### `options`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:33](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L33)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
CacheOptionsType<ClientInstance>;
```

</div><hr/></div><div class="api-docs__property" property-data="storage"><h3 class="api-docs__name">

### `storage`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:27](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L27)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
CacheStorageType;
```

</div><hr/></div></div><div class="api-docs__section">

## Methods

</div><div class="api-docs__methods"><div class="api-docs__method" method-data="clear"><h3 class="api-docs__name">

### `clear()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
clear();
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Clear cache storages

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:237](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L237)

</p><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<void>;
```

</div><hr/></div><div class="api-docs__method" method-data="delete"><h3 class="api-docs__name">

### `delete()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
delete cacheKey;
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Delete record from storages and trigger revalidation

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:124](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L124)

</p><div class="api-docs__section">

#### Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
void
```

</div><hr/></div><div class="api-docs__method" method-data="get"><h3 class="api-docs__name">

### `get()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
get<Response, Error, Adapter>(cacheKey);
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Get particular record from storage by cacheKey. It will trigger lazyStorage to emit lazy load event for reading
it&#x27;s data.

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:102](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L102)

</p><div class="api-docs__section">

#### Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
{
  data: GenericDataType | null;
  error: GenericErrorType | null;
  extra: T extends AdapterType<any, any, any, infer A, any> ? A : never | null;
  status: T extends AdapterType<any, any, infer S, any, any> ? S : never | null;
  success: boolean;
  isCanceled: boolean;
  isOffline: boolean;
  retries: number;
  timestamp: number;
  cacheTime: number;
  clearKey: string;
  garbageCollection: number;
}
```

</div><hr/></div><div class="api-docs__method" method-data="getLazyKeys"><h3 class="api-docs__name">

### `getLazyKeys()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
getLazyKeys();
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Used to receive keys from sync storage and lazy storage

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:192](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L192)

</p><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<string[]>;
```

</div><hr/></div><div class="api-docs__method" method-data="getLazyResource"><h3 class="api-docs__name">

### `getLazyResource()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
getLazyResource<Response, Error, Adapter>(cacheKey);
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Used to receive data from lazy storage

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:157](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L157)

</p><div class="api-docs__section">

#### Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<CacheValueType<Response, Error, Adapter>>;
```

</div><hr/></div><div class="api-docs__method" method-data="keys"><h3 class="api-docs__name">

### `keys()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
keys();
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Get sync storage keys, lazyStorage keys will not be included

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:114](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L114)

</p><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
string[]
```

</div><hr/></div><div class="api-docs__method" method-data="revalidate"><h3 class="api-docs__name">

### `revalidate()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
revalidate(cacheKey);
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Revalidate cache by cacheKey or partial matching with RegExp

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:135](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L135)

</p><div class="api-docs__section">

#### Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey

`Required`

</td><td class="api-docs__param-type">

`string | RegExp`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<void>;
```

</div><hr/></div><div class="api-docs__method" method-data="scheduleGarbageCollector"><h3 class="api-docs__name">

### `scheduleGarbageCollector()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
scheduleGarbageCollector(cacheKey);
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Schedule garbage collection for given key

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:205](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L205)

</p><div class="api-docs__section">

#### Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<void>;
```

</div><hr/></div><div class="api-docs__method" method-data="set"><h3 class="api-docs__name">

### `set()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
set<Response, Error>(request, response);
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Set the cache data to the storage

</span></div><p class="api-docs__definition">

Defined in
[cache/cache.ts:61](https://github.com/BetterTyped/hyper-fetch/blob/c3e2c45/packages/core/src/cache/cache.ts#L61)

</p><div class="api-docs__section">

#### Parameters

</div>
<div class="api-docs__parameters">
<table>
<thead><tr><th>Name</th><th>Details</th></tr></thead>
<tbody><tr param-data="request"><td class="api-docs__param-name required">

#### request

`Required`

</td><td class="api-docs__param-type">

`RequestInstance | RequestJSON<RequestInstance, unknown, QueryParamsType, null>`

</td></tr><tr param-data="response"><td class="api-docs__param-name required">

#### response

`Required`

</td><td class="api-docs__param-type">

`ResponseReturnType<Response, Error, ExtractClientAdapterType<C>> & ResponseDetailsType`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
void
```

</div><hr/></div></div>
